package 
{
	import application.ApplicationFacade;
	import application.model.VO.LayoutVO;
	import application.view.viewComponent.GroupCombobox;
	import application.view.viewComponent.ILayout;
	import application.view.viewComponent.LogoView;
	import application.view.viewComponent.MainNavView;
	import application.view.viewComponent.ModuleView;
	import application.view.viewComponent.PhotoArea;
	import application.view.viewComponent.PhotoView;
	import application.view.viewComponent.TipView;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.MouseEvent;
	import ginger.events.DataEvent;
	import ginger.ui.simpleControls.SimpleBrightnessButton;
	import ginger.ui.simpleControls.SimpleLayout;
	import ginger.utils.DisplayUtil;
	import flash.display.Sprite;
	import flash.events.Event;
	import org.puremvc.as3.patterns.facade.Facade;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import caurina.transitions.Tweener;

	/**
	 * ...
	 * @author Ning
	 */
	[Frame(factoryClass="Preloader")]
	public class Application extends Sprite 
	{
		//选择组控件
		private var _groupCombobox:GroupCombobox;
		//照片区域
		private var _photoArea:PhotoArea;
		//照片视图
		private var _photoView:PhotoView;
		//模块导航
		private var _mainNavView:MainNavView;
		//模块视图
		private var _moduleView:ModuleView;
		//logo
		private var _logoView:LogoView;
		
		//提示按钮
		private var _tipView:TipView;
		
		
		private var _facade:ApplicationFacade;
		
		public function get groupCombobox():GroupCombobox {
			return _groupCombobox;
		}
		public function get photoArea():PhotoArea {
			return _photoArea;
		}
		public function get photoView():PhotoView {
			return _photoView;
		}
		public function get mainNavView():MainNavView {
			return _mainNavView;
		}
		public function get moduleView():ModuleView {
			return _moduleView;
		}
		
		public function Application():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			stage.addEventListener(Event.RESIZE, onResize)
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
				
			AppGlobal.STAGE = stage;
			
			_photoArea = new PhotoArea();
			addChild(_photoArea);
			
			_groupCombobox = new GroupCombobox();
			addChild(_groupCombobox);
			
			_mainNavView = new MainNavView();
			addChild(_mainNavView);
			
			_moduleView = new ModuleView();
			
			_photoView = new PhotoView();
			_mainNavView.addEventListener(Event.SELECT, onNavSelect);
			
			_logoView = new LogoView();
			addChild(_logoView);
			
			_tipView = new TipView();
			addChild(_tipView);
			
			_facade = ApplicationFacade.getInstance();
			_facade.startup(this);
		}
		
		public function updateLayoutConfig(config:*):void
		{
			_logoView.updateLayout(config[AppGlobal.LOGO_LAYOUT_KEY]);
			_tipView.updateLayout(config[AppGlobal.TIP_ICON_LAYOUT]);
		}
		
		/**
		 * 展示图片区
		 */
		public function showPhotoArea():void
		{
			if (!_photoView.hasEventListener(PhotoView.LEAVE_COMPLETE))
			    _photoView.addEventListener(PhotoView.LEAVE_COMPLETE, onPhotoViewLeaveComplete);
			_photoView.leave();
		}
		private function onPhotoViewLeaveComplete(evt:Event):void
		{
			DisplayUtil.removeAllChild(this);
			addChild(_photoArea);
			    _photoArea.updateAreaLayout();
            addChild(_logoView);
			    _logoView.show();
		    addChild(_groupCombobox);
			    _groupCombobox.show();
			addChild(_mainNavView);
			    _mainNavView.show();
			addChild(_tipView)
			    _tipView.show();
			
		}
		
		/**
		 * 展示图片
		 */
		public function showPhoto():void
		{
			if (!_photoArea.hasEventListener(PhotoArea.LEAVE_COMPLETE))
			    _photoArea.addEventListener(PhotoArea.LEAVE_COMPLETE, onPhotoAreaLeaveComplete);
			_photoArea.leave();
			_logoView.leave();
			_groupCombobox.leave();
			_mainNavView.leave();
			_tipView.leave();
		}
		private function onPhotoAreaLeaveComplete(evt:Event):void
		{
			_photoArea.removeEventListener(PhotoArea.LEAVE_COMPLETE, onPhotoAreaLeaveComplete);
			DisplayUtil.removeAllChild(this);
			addChild(_photoView);
			_photoView.show();
			_photoView.alpha = 0;
			Tweener.addTween(_photoView, {time:1,alpha:1 } );
		}
		
		private function onResize(evt:Event = null):void
		{
			if (_photoArea.lock)
				onPhotoAreaMove();

			for (var i:int = 0; i < this.numChildren; i++ )
			{
				var vc:ILayout = getChildAt(i) as ILayout;
				if (vc) {
					vc.layout();
				}
			}
		}
		private function onNavSelect(evt:DataEvent):void
		{
			if (!contains(_moduleView)) {
			    addChild(_moduleView);
				_moduleView.x = -_moduleView.moduleWidth;
				Tweener.addTween(_moduleView, { time:.5, x:0 } );
				Tweener.addTween(_photoArea, { time:.5, x:_moduleView.moduleWidth } );
			}
			_moduleView.showByName(String(evt.data));
			
			_photoArea.lock = true;
			if(!_photoArea.hasEventListener(MouseEvent.MOUSE_MOVE))
			    _photoArea.addEventListener(MouseEvent.MOUSE_MOVE, onPhotoAreaMove);
		}
		private function onPhotoAreaMove(evt:MouseEvent=null):void
		{
			_photoArea.removeEventListener(MouseEvent.MOUSE_MOVE, onPhotoAreaMove);
			_photoArea.lock = false;
			if (contains(_moduleView))
			    removeChild(_moduleView);
			AppGlobal.STAGE.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_MOVE));
		}
	}
}